<?php

namespace App\Http\Controllers;

use Auth;
use App\Models\User;
use App\Models\UserAddress;
use Illuminate\Http\Request;
use App\Http\Requests\UserAddressRequest;

class UserAddressesController extends Controller{
    public function index(Request $request) {
        $addresses = $request->user()->addresses;
        return view('user_addresses.index',compact('addresses'));
    }

    public function create() {
        return view('user_addresses.create_and_edit',['address' => new UserAddress()]);
    }

    public function store(UserAddressRequest $request) {
        $request->user()->addresses()->create($request->only([
            'province',
            'city',
            'district',
            'address',
            'zip',
            'contact_name',
            'contact_phone',
        ]));
        return redirect()->route('user_addresses.index');
    }

    public function edit(UserAddress $user_address) {
        $this->authorize('own',$user_address);
        return view('user_addresses.create_and_edit',['address' => $user_address]);
    }

    public function update(UserAddressRequest $request,UserAddress $user_address) {
        $this->authorize('own',$user_address);
        $user_address->update($request->only([
            'province',
            'city',
            'district',
            'address',
            'zip',
            'contact_name',
            'contact_phone',
        ]));

        return redirect()->route('user_addresses.index');
    }

    public function destroy(UserAddress $user_address) {
        $this->authorize('own',$user_address);
        $user_address->delete();
        // 把之前的 redirect 改成返回空数组
        // return redirect()->route('user_addresses.index');
        return [];
    }
}
